package com.xizi.lotterySystem.common.interceptor;

import com.xizi.lotterySystem.common.pojo.Constants;
import com.xizi.lotterySystem.utils.JWTUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.ErrorResponse;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader(Constants.LOGIN_USER_TOKEN);
        log.info("user_token:{}", token);

        if (token == null || JWTUtil.parseJWT(token) == null) {
            response.setStatus(401);
            log.error("拦截成功！");
            return false;
        }

        log.info("拦截失败！");
        return true;
    }
}
